package com.boxuegu.guanweihua25.boxuegudatabinding.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableField;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.boxuegu.guanweihua25.boxuegudatabinding.R;
import com.boxuegu.guanweihua25.boxuegudatabinding.apis.Apis;
import com.boxuegu.guanweihua25.boxuegudatabinding.databinding.ActivityRegisterBinding;
import com.boxuegu.guanweihua25.boxuegudatabinding.model.RegisterModel;
import com.boxuegu.guanweihua25.boxuegudatabinding.utils.RetrofitUtils;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RegisterActivity extends AppCompatActivity {
    ObservableField<String> username=new ObservableField<>();
    ObservableField<String>password=new ObservableField<>();
    ObservableField<String>repassword=new ObservableField<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityRegisterBinding binding= DataBindingUtil.setContentView(this,
                R.layout.activity_register);
//        setContentView(R.layout.activity_register);
        binding.setUsername(username);
        binding.setPassword(password);
        binding.setRepassword(repassword);
        binding.setMyclick(new MyClick());
        binding.img3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    public class MyClick{
        public void onClick(View view){
            Call<RegisterModel> registerModelCall= RetrofitUtils.getRetrofit().create(Apis.class)
                                                    .getRegister(username.get(),password.get(),
                                                            repassword.get());
            if (TextUtils.isEmpty(username.get())){
                Toast.makeText(RegisterActivity.this,"请输入用户名", Toast.LENGTH_LONG).show();
                return;
            }
            if (password.get().equals(repassword.get())){
                registerModelCall.enqueue(new Callback<RegisterModel>() {
                    @Override
                    public void onResponse(Call<RegisterModel> call, Response<RegisterModel> response) {
                        RegisterModel registerModel=response.body();
                        if(response.body().code==0){
                            Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_LONG).show();
                            Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
                            startActivity(intent);
                        }else{
                            //弹框提示
                            Toast.makeText(RegisterActivity.this,"注册失败",Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<RegisterModel> call, Throwable t) {

                    }
                });

            }else{
                Toast.makeText(RegisterActivity.this,"密码不一致，请重新输入",Toast.LENGTH_LONG).show();
            }
            if (TextUtils.isEmpty(password.get()) ||TextUtils.isEmpty(repassword.get())) {
                Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_LONG).show();
                return;
            }

        }
    }
}
